package com.hand.qt24953.order.app.service.impl;

import com.hand.qt24953.order.app.service.SoHeaderService;
import com.hand.qt24953.order.domain.entity.SoHeader;
import com.hand.qt24953.order.domain.entity.SoLine;
import com.hand.qt24953.order.domain.repository.SoHeaderRepository;
import com.hand.qt24953.order.domain.repository.SoLineRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售订单头应用服务默认实现
 *
 * @author tao.qiu01@hadn-china.com 2019-08-02 11:30:00
 */
@Service
public class SoHeaderServiceImpl implements SoHeaderService {
    private final SoHeaderRepository soHeaderRepository;
    private final SoLineRepository soLineRepository;

    public SoHeaderServiceImpl(SoHeaderRepository soHeaderRepository, SoLineRepository soLineRepository) {
        this.soHeaderRepository = soHeaderRepository;
        this.soLineRepository = soLineRepository;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOrder(SoHeader soHeader) {
        List<SoLine> soLineList = soLineRepository.select(SoLine.FIELD_SO_HEADER_ID, soHeader.getSoHeaderId());
        soHeaderRepository.deleteByPrimaryKey(soHeader);
        soLineRepository.batchDeleteByPrimaryKey(soLineList);
    }
}
